﻿#pragma once

#ifndef NOMINMAX
#    define NOMINMAX
#endif

#ifdef _MSC_VER
#    ifdef BUILD_LIB
#        define LIB_API __declspec(dllexport)
#    else
#        define LIB_API   //__declspec(dllimport)
#    endif
#else
#    define LIB_API
#endif

////////////////////////////////////////////////////////////////////////////////
// c/c++ standard
// #include <algorithm>
// #include <ctime>
// #include <fstream>
#include <filesystem>
#include <iostream>
#include <thread>
// #include <vector>

#ifdef _MSC_VER
#    define LOCALTIME(time, result) localtime_s(&result, &time)
#else
#    define LOCALTIME(time, result) localtime_r(&time, &result)
#endif

////////////////////////////////////////////////////////////////////////////////
// boost
#ifdef USB_BOOST_ASIO
#    include <boost/asio.hpp>
namespace asio = boost::asio;            // from <boost/asio.hpp>
using tcp      = boost::asio::ip::tcp;   // from <boost/asio/ip/tcp.hpp>
#endif

////////////////////////////////////////////////////////////////////////////////
// Qt
#ifdef USE_LIB_QT5
#    ifdef QT_WIDGETS_LIB
#        include <QApplication>
#        include <QDialog>
#        include <QFileDialog>
#        include <QMainWindow>
#        include <QMessageBox>
#    endif

#    ifdef QT_GUI_LIB
#        include <QCloseEvent>
#    endif

#    ifdef QT_CORE_LIB
#        include <QBuffer>
#        include <QTranslator>
#    endif
#endif

////////////////////////////////////////////////////////////////////////////////
// sqlite_orm
#ifdef USE_LIB_SQLITE_ORM
#    pragma warning(push)
#    pragma warning(disable : 4996)
#    include <sqlite_orm/sqlite_orm.h>
#    pragma warning(pop)
#endif

////////////////////////////////////////////////////////////////////////////////
// libusb
#ifdef USE_LIB_LIBUSB
#    pragma warning(push)
#    pragma warning(disable : 4200)
#    ifdef _MSC_VER
#        include <Winsock2.h>
#    endif
#    include <libusb.h>
#    pragma warning(pop)
#endif

////////////////////////////////////////////////////////////////////////////////
// libzmq
#ifdef USE_LIB_ZMQ
#    include <zmq.hpp>
#endif

////////////////////////////////////////////////////////////////////////////////
// alglib
#ifdef USE_LIB_ALGLIB
#    ifdef _MSC_VER
#        pragma warning(push)
// #        pragma warning(disable : 4706)
#    else
#        pragma GCC diagnostic push
#        pragma GCC diagnostic ignored "-Wvolatile"
#        pragma GCC diagnostic ignored "-Wunused-function"
#    endif
#    define ALGLIB_NO_FAST_KERNELS
#    include <interpolation.h>
#    ifdef _MSC_VER
#        pragma warning(pop)
#    else
#        pragma GCC diagnostic pop
#    endif
#endif

////////////////////////////////////////////////////////////////////////////////
// opencv
#ifdef USE_LIB_OPENCV
#    pragma warning(push)
#    pragma warning(disable : 4127)
#    include <opencv2/core/core_c.h>   // shame, but needed for using dlib
#    include <opencv2/core/types_c.h>
#    include <opencv2/imgproc/types_c.h>
#    include <opencv2/opencv.hpp>
#    include <opencv2/videoio/legacy/constants_c.h>
#    pragma warning(pop)

#endif

////////////////////////////////////////////////////////////////////////////////
// fmt
#ifdef USE_LIB_FMT
#    include <fmt/chrono.h>
#    include <fmt/core.h>
#    include <fmt/ranges.h>
#endif

////////////////////////////////////////////////////////////////////////////////
// msgpack
#ifdef USE_LIB_MSGPACK
#    ifdef _MSC_VER
#        pragma warning(push)
#        pragma warning(disable : 4005)
#    else
#        pragma GCC diagnostic push
#    endif
#    ifndef USE_LIB_BOOST
#        define MSGPACK_NO_BOOST
#    endif
#    include <msgpack.hpp>
#    ifdef _MSC_VER
#        pragma warning(pop)
#    else
#        pragma GCC diagnostic pop
#    endif
#endif

////////////////////////////////////////////////////////////////////////////////
// cpr and libcurl
#ifdef USE_LIB_CPR
#    ifdef _MSC_VER
#        pragma warning(push)
#        pragma warning(disable : 4459)
#    else
#        pragma GCC diagnostic push
// #    pragma GCC diagnostic ignored "-Wsign-compare"
#    endif
#    include "cpr/cpr.h"
#    ifdef _MSC_VER
#        pragma warning(pop)
#    else
#        pragma GCC diagnostic pop
#    endif
#endif

////////////////////////////////////////////////////////////////////////////////
// cinatra
// #ifdef USE_LIB_CINATRAL
// #    include <cinatra.hpp>
// #endif

////////////////////////////////////////////////////////////////////////////////
// simple_match
#ifdef USE_LIB_SIMPLE_MATCH
#    include <simple_match/simple_match.hpp>
using namespace simple_match;
using namespace simple_match::placeholders;
#endif

////////////////////////////////////////////////////////////////////////////////
// GLFW
#ifdef USE_LIB_GLFW
#    if defined(_WIN32)
#        include <windows.h>
#    endif
#    include <GLFW/glfw3.h>   // Will drag system OpenGL headers
#endif

////////////////////////////////////////////////////////////////////////////////
// ImGui
#ifdef USE_LIB_IMGUI
#    ifndef IMGUI_DEFINE_MATH_OPERATORS
#        define IMGUI_DEFINE_MATH_OPERATORS
#    endif
#    include "imgui.h"
#    include "imgui_internal.h"

// #    include "imgui_impl_glfw.h"
// #    include "imgui_impl_opengl2.h"
#endif

////////////////////////////////////////////////////////////////////////////////
// SDL
#ifdef USE_LIB_SDL
#    include <SDL.h>
#    if defined(IMGUI_IMPL_OPENGL_ES2)
#        include <SDL_opengles2.h>
#    else
#        include <SDL_opengl.h>
#    endif
#    ifdef USE_LIB_IMGUI
#        include "imgui_impl_sdl2.h"
#        include "imgui_impl_sdlrenderer2.h"
#    endif
#endif

////////////////////////////////////////////////////////////////////////////////
// utils
#ifdef USE_LIB_BOOST
#    include "logger.h"
#endif

// 探头在 1.5 公斤力校准时，换算成压强
constexpr double PRESSURE_15 = 34.72;

// PI值
constexpr double PI = 3.14159265358979323846;

// 密码盐
#define PWD_SALT "63UrCwJhTH"
constexpr std::string_view ENC_KEY = "UrCwJhTH63";